<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use App\Models\User;
use App\Models\Store;
use App\Models\Order;
use Illuminate\Support\Facades\Auth;

class TestController extends ApiController
{
    /**
     * 测试
     */

     /**
      * 查询个人审核信息
      */
    public function index(Request $request)
    {
        $limit = $request->input('size', 15);
        $myuser = User::select()->where('check', 1);  //选择未认证的个人信息
        return $this->success($myuser->paginate($limit));
        //return $this->success(User::paginate($limit)->where('check', 0));
    }

    /**
     * 查询店铺审核信息
     */
    public function storesInfo(Request $request)
    {
        $limit = $request->input('size', 15);
        $mystore = Store::select()->with(['user'])->where('check', 1);  //选择未认证的个人信息
        return $this->success($mystore->paginate($limit));
        //return $this->success(User::paginate($limit)->where('check', 0));
    }
  

    /**
     * 单个用户
     */
    public function show($id, Request $request) {
        return $this->success(User::find($id));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        //$request->input('bar');

        return $this->success(Auth::guard('manager')->user());
    }

    /**
     * 个人审核
     */
    public function check($id, Request $request){
        $check = $request->input('check');
        // 做一些验证
        $user = User::find($id);

        if(!$user) {
            return $this->failed('没有此用户');
        }

        // 传递的check 为 1 审核通过
        if ($check === 2) {
            //if(!in_array($user->check, [-1 ,1])) {
            if($user->check != 1) {
                return $this->failed('此状态不允许审核通过');
            }

            $user->check = 2;
            if ($user->save()) {
                return $this->success('个人验证通过了审核');
            } else {
                return $this->failed('数据保存失败，请重试');
            }
        }

        if ($check === -1 ) {
            // TODO
            if($user->check != 1) {
                return $this->failed('此状态不允许审核通过');
            }

            $user->check = -1;
            if ($user->save()) {
                return $this->success('该用户未能通过审核');
            } else {
                return $this->failed('数据保存失败，请重试');
            }
        }

        return $this->failed('出错');
    }
 
    /**
     * 店铺审核
     */
    public function storecheck($id, Request $request){
        $check = $request->input('check');
        // 做一些验证
        $mystore = Store::find($id);

        if(!$mystore) {
            return $this->failed('没有此店铺');
        }

        // 传递的check 为 1 审核通过
        if ($check === 2) {
            //if(!in_array($mystore->check, [-1 ,1])) {
            if($mystore->check != 1) {
                return $this->failed('此状态不允许审核通过');
            }

            $mystore->check = 2;
            if ($mystore->save()) {
                return $this->success('该店铺通过了审核');
            } else {
                return $this->failed('数据保存失败，请重试');
            }
        }

        if ($check === -1 ) {
            // TODO
            if($mystore->check != 1) {
                return $this->failed('此状态不允许审核通过');
            }

            $mystore->check = -1;
            if ($mystore->save()) {
                return $this->success('该店铺未能通过审核');
            } else {
                return $this->failed('数据保存失败，请重试');
            }
        }

        return $this->failed('出错');
    }

}
